/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;

import entity.UsuarioFacade;
import entity.UsuarioFacadeLocal;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Bryan Valencia
 */
public class Usuario extends HttpServlet {
    
    @EJB
    private UsuarioFacadeLocal usuarioFacade = new UsuarioFacade();

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Usuario</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet Usuario at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        entity.Usuario u = usuarioFacade.find((request.getParameter("id")));
        response.setContentType("application/json; charset=UTF-8;");
        PrintWriter out = response.getWriter();
        if(u == null){
            out.println("{\"result\":\"vacio\"}");
        }
        else{            
            out.println("{"
                    + "\"nombre\":\""+u.getNombre()+"\","
                    + "\"cedula\":\""+u.getCedula()+"\""
                    + "}");            
        }        
        
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        HttpSession session = request.getSession();
        String id = request.getParameter("cedulausuario");
        String nombre = request.getParameter("nombreusuario");        
        /*String descripcion = request.getParameter("descripcion");        
        String costo = request.getParameter("costo-producto");
        String cantidad = request.getParameter("cantidad-producto");*/
        String accion = request.getParameter("accion");        
        entity.Usuario usuario;                
        if(id.isEmpty() || nombre.isEmpty() /*|| descripcion.isEmpty() || costo.isEmpty() || cantidad.isEmpty()*/){                        
            //request.getRequestDispatcher("faces/admin/index.xhtml#inventario-4").forward(request, response);
            //response.sendRedirect("faces/admin/index.xhtml#inventario-4");
        }else if(accion.equals("Crear")){
            /*usuario = new entity.Usuario();            
            usuarioFacade.create(usuario);*/
        }else if(accion.equals("Modificar")){            
            usuario = usuarioFacade.find(id);            
            //usuario.setCedula(id);
            usuario.setNombre(nombre);
            usuarioFacade.edit(usuario);                
            
        }else if(accion.equals("Borrar")){                                      
            usuario = usuarioFacade.find(id);            
            usuarioFacade.remove(usuario);                      
        }            
        
        //session.setAttribute("materiales", usuarioFacade.findAll());
        response.sendRedirect("faces/admin/index.xhtml#usuarios-6");     
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
